home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 7 / BBS in a Box - Macintosh - Volume VII (BBS in a Box) (January 1993).iso / Files / Prog / T / TC Prog Guide.cpt / picture ƒ / camera.c < prev    next >
Text File  |  1990-10-15  |  2KB  |  87 lines

  1. /*
  2. *    FILE:        camera.c
  3. *    AUTHOR:        R. Gonzalez
  4. *    CREATED:    October 3, 1990
  5. *
  6. *    Methods for camera class, which defines the view location.
  7. */
  8.  
  9. # include    "camera.h"
  10.  
  11. /******************************************************************
  12. *    initialize
  13. ******************************************************************/
  14. boolean    Camera::init(void)
  15. {
  16.     world_to_camera = new(World_To_Camera);
  17.     world_to_camera->init();
  18.     
  19.     set_position(0.,0.,0.,0.,0.,0.);
  20.     set_focal_length(1.);
  21.     
  22.     return TRUE;
  23. }
  24.  
  25. /******************************************************************
  26. *    set location and orientation of camera using world coordinates.
  27. *    See camera.h for description.
  28. ******************************************************************/
  29. void    Camera::set_position(double x,double y,double z,
  30.                     double yaw,double pitch,double roll)
  31. {
  32.     world_to_camera->set(x,y,z,yaw,pitch,roll);
  33. }
  34.  
  35. /******************************************************************
  36. *    set focal length of camera. This can be used for "zoom" effect.
  37. *    Represents distance from camera origin to focal plane.
  38. ******************************************************************/
  39. void    Camera::set_focal_length(double f)
  40. {
  41.     focal_length = f;
  42. }
  43.  
  44. /******************************************************************
  45. *    convert 3D world coordinate to 2D image on focal plane.
  46. *    Returns FALSE if thing is in back of camera.
  47. ******************************************************************/
  48. boolean    Camera::take_photo(Coord2 *image,Coord3 *thing)
  49. {
  50.     boolean    success = TRUE;
  51.     Coord3    *temp;
  52.     
  53.     temp = new(Coord3);
  54.     temp->init();
  55.     temp->apply(thing,world_to_camera);
  56.     
  57.     if (temp->z < 1e-4 && temp->z > -1e-4)
  58.     {
  59.         image->x = temp->x * focal_length/(temp->z+1e-4);
  60.         image->y = temp->y * focal_length/(temp->z+1e-4);
  61.     }
  62.     else
  63.     {
  64.         image->x = temp->x * focal_length/temp->z;
  65.         image->y = temp->y * focal_length/temp->z;
  66.     }
  67.     
  68.     if (temp->z < 1e-4)
  69.         success = FALSE;
  70.         
  71.     temp->destroy();
  72.     delete(temp);
  73.     
  74.     return success;
  75. }
  76.  
  77. /******************************************************************
  78. *    destroy
  79. ******************************************************************/
  80. boolean    Camera::destroy(void)
  81. {
  82.     world_to_camera->destroy();
  83.     delete(world_to_camera);
  84.     
  85.     return TRUE;
  86. }
  87.